package com.example.demo.controller;
import com.example.demo.domain.result.ExamInfoResult;
import com.example.demo.domain.result.GradesResult;
import com.example.demo.exception.PasswordErrorException;
import com.example.demo.result.ApiResult;
import com.example.demo.result.ResultCode;
import com.example.demo.service.GradesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author hongjinhui
 */
@RestController
@RequestMapping("/School/user")
@Slf4j
@Api("用户接口")
public class UserController {
    @Autowired
    private GradesService gradesService;

    @ApiOperation(value = "查询某学期成绩")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "教务系统账号",required = true,paramType = "query"),
    @ApiImplicitParam(name = "password",value = "教务系统密码",required = true,paramType = "query"),
    @ApiImplicitParam(name = "termId",value = "学期id,不传则返回当前学期成绩",required = false,paramType = "query"),
            @ApiImplicitParam(name = "campusId",value = "为1返回湖工成绩，为2返回工程成绩",required = true,paramType = "query")})

    @GetMapping("/grades")
    public ApiResult<GradesResult> grades(@NotNull(message = "username不能为空") String username,
                                          @NotNull(message = "password不能为空") String password, String termId,@RequestParam(required = false,defaultValue = "1") Integer campusId) throws PasswordErrorException, IOException {
        if (campusId == 2) {
            return ApiResult.success(gradesService.getEtcGrade(username, password, termId));
        } else {
            return ApiResult.success(gradesService.getGrades(username, password, termId));
        }
    }

    @ApiOperation(value = "查询考试信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "教务系统账号", required = true, paramType = "query"),
                        @ApiImplicitParam(name = "password", value = "教务系统密码", required = true, paramType = "query"),})
    @GetMapping("/examInfo")
    public ApiResult<List<ExamInfoResult>> examInfo(@NotNull(message = "username不能为空") String username,
                                    @NotNull(message = "password不能为空") String password) throws PasswordErrorException, IOException {
        return ApiResult.success(gradesService.queryExamInfo(username, password));
    }
}

